package edu.wayne.cs.csc4110.hw6.framework.gui.lobby;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import edu.wayne.cs.csc4110.hw6.framework.Util.Constants;
import edu.wayne.cs.csc4110.hw6.framework.gui.inbox.PrivateChatFrame;
import edu.wayne.cs.csc4110.hw6.framework.lobby.GameLobbyServer;
import edu.wayne.cs.csc4110.hw6.interfaces.message.Message;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class ChatArea extends JPanel {
	// Text Area for chat
	private JTextPane textPane = new JTextPane();
	private JScrollPane scrollPane = new JScrollPane(textPane);

	// User List and User ListModel
	private JList<User> userList;
	private JScrollPane userListScrollPane;
	private DefaultListModel<User> model;

	// Popup menu and menu items
	private JPopupMenu popup;
	private JMenuItem menuItemSendMsg;
	private JMenuItem menuItemProfile;

	public void initialize() {
		popup = new JPopupMenu();
		model = new DefaultListModel<User>();
		userList = new JList<User>(model);
		userListScrollPane = new JScrollPane(userList);
		menuItemSendMsg = new JMenuItem("Send Private Message");
		menuItemSendMsg.setActionCommand("sendMsg");

		userList.setPreferredSize(Constants.USER_LIST_PREFERRED_SIZE);
		this.setPreferredSize(Constants.CHAT_AREA_PREFERRED_SIZE);
		textPane.setPreferredSize(Constants.TEXT_PANE_PREFERRED_SIZE);
		userListScrollPane.setPreferredSize(Constants.USER_LIST_PREFERRED_SIZE);
		textPane.setText("");
		textPane.setEditable(false);

	}

	public ChatArea(final User user) {
		initialize();
		userList.setCellRenderer(new UserListCellRender());
		popup.add(menuItemSendMsg);
		userList.setComponentPopupMenu(popup);
		userList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.isPopupTrigger()) {
					userList.setSelectedIndex(getListRow(e.getPoint()));
					popup.show(userList, e.getX(), e.getY());
				}
			}

		});

		menuItemSendMsg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("sendMsg")) {
					new PrivateChatFrame(user);
				}
			}

		});

		setupUserList();
		this.add(scrollPane);
		this.add(userListScrollPane);

	}

	private void setupUserList() {
		for (User user : GameLobbyServer.getInstance().getUsers()) {
			model.addElement(user);
		}
	}

	public void appendMessage(Message msg) {
		try {
			String strMsg = msg.getFormatedMessage() + "\n";
			Document doc = textPane.getDocument();
			doc.insertString(doc.getLength(), strMsg, null);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		}
	}

	public void updateUserList(User u) {
		if (model.contains(u)) {
			model.removeElement(u);
		} else {
			model.addElement(u);
		}

	}

	private int getListRow(Point point) {
		return userList.locationToIndex(point);
	}

	public class UserListCellRender extends DefaultListCellRenderer {
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			super.getListCellRendererComponent(list, value, index, isSelected,
					cellHasFocus);

			if (value != null) {
				User user = (User) value;
				setText(user.getUsername());
			}

			return this;
		}

	}
}
